<script setup lang="ts">
import { ref } from "vue";
import { useI18n } from 'vue-i18n';
import ReCol from "@/components/ReCol";
import { FormProps } from "./logic/types";
import { initValues, rules } from "./logic/form";
import { handleTree } from "@/utils/tree";

const { t } = useI18n();

const props = withDefaults(defineProps<FormProps>(), {
  formData: () => initValues(),
  list: []
});

const formRef = ref();
const formData = ref(props.formData);

const getRef = () => formRef.value;

const treeList = handleTree(props.list);
defineExpose({ getRef });
</script>

<template>
  <el-form ref="formRef" :model="formData" :rules="rules" label-width="92px">
    <el-row :gutter="30">
      <re-col :value="24" :xs="24" :sm="24">
        <el-form-item :label="t('sys.deptPage.form.parentId')" prop="parentId">
          <el-cascader
            v-model="formData.parentId"
            class="w-full"
            :options="treeList"
            :props="{
              value: 'id',
              label: 'name',
              emitPath: false,
              checkStrictly: true
            }"
            clearable
            filterable
            :placeholder="t('sys.deptPage.form.placeholder.parentId')"
          >
            <template #default="{ node, data }">
              <span>{{ data.name }}</span>
              <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
            </template>
          </el-cascader>
        </el-form-item>
      </re-col>

      <re-col :value="24" :xs="24" :sm="24">
        <el-form-item :label="t('sys.deptPage.form.name')" prop="name">
          <el-input
            v-model="formData.name"
            show-word-limit
            maxlength="30"
            clearable
            :placeholder="t('sys.deptPage.form.placeholder.name')"
          />
        </el-form-item>
      </re-col>

      <re-col :value="24" :xs="24" :sm="24">
        <el-form-item :label="t('sys.deptPage.form.rank')" prop="rank">
          <el-input-number
            v-model="formData.rank"
            :min="0"
            :max="9999"
            controls-position="right"
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
